const db = wx.cloud.database();
const _ = db.command
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    faver: [],
    moreshow: false,
    moretit: true,
    issc: '收藏',
    list: [],
    job: [],
    jobList: [],
    id: '',
    isClick: false,
    jobStorage: [],
    jobId: '',
    collectNum: 0
  },
  go_jobInfo: function (t) {
    var z = t.currentTarget.dataset.zdy,
      a = t.currentTarget.dataset.index,
      i = this.data.list;
    i[a].zwk_jishu++, this.setData({
      list: i
    }), wx.navigateTo({
      url: "../search/jobInfo/jobInfo?zdy=" + z 
    });
  },
  haveSave(e) {

    let that = this
    let index = 0;
    let array = this.data.list; //获取循环数组对象
    for (let item of array) {
      //如果当前点击的对象id和循环对象里的id一致
      if (item.id == e.currentTarget.dataset.id) {

        if (array[index].fav == "" || array[index].fav == undefined) {
          array[index].fav = "true";
          console.log(item)

          db.collection('bluserInfo').where({
            _openid: app.globalData.openid,
          }).update({
            data: {
              fav: _.push(item)
            }
          })

          wx.showToast({
            title: '已收藏',
          });
        }
      }
      index++;
    }

    this.setData({
      list: array
    })
  },
  clear_collect: function () {
    wx.setStorageSync("collectNum", 0), this.setData({
      collectNum: wx.getStorageSync("collectNum")
    }), wx.navigateTo({
      url: "../search/myJob/myJob"
    });
  },
  addJob: function (t) {
    var a = this,
      i = t.currentTarget.dataset.id,
      s = this.data.list,
      o = t.currentTarget.dataset.index;

    function objFn(obj, objIndex, objs) {
      return obj.id == i;
    }
    var index = s.findIndex(objFn);
    console.log(s[index]);
    a.data.faver.push(s[index]);
    wx.setStorageSync('faver', a.data.faver)

    db.collection('bluserfav').add({
      data: {
        count: s[index]
      },
      success: res => {
        console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)
        wx.showToast({
          title: "添加成功",
          icon: "none"
        }), s[o].storage_status = 1, a.setData({
          list: s
        }), wx.setStorageSync("collectNum", a.data.collectNum + 1), a.setData({
          collectNum: wx.getStorageSync("collectNum")
        })
      },
      fail: err => {
        console.error('[数据库] [新增记录] 失败：', err)
      }
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(wx.getStorageSync('theJson'));
    let theJson = wx.getStorageSync('theJson')
    that.setData({
      list: theJson
    })
    console.log(that.data.list)
  },
  clase: function (e) {
    let that = this
    let index = 0;
    let array = this.data.list; //获取循环数组对象
    for (let item of array) {
      //如果当前点击的对象id和循环对象里的id一致
      if (item.id == e.currentTarget.dataset.id) {
        //判断当前对象中的insert是否为true（true为显示，其他为隐藏） insert是新增的一个值然后进行判断
        if (array[index].insert == "" || array[index].insert == undefined) {
          array[index].insert = "true";
        } else {
          array[index].insert = "";
        }
      }
      index++;
    }
    //将数据动态绑定 
    that.setData({
      list: array
    })
  },
  gofav: function () {
    db.collection('bluserInfo').where({
      _openid: app.globalData.openid,
    }).get({
      success: function (res) {
        console.log(res.data[0].fav)
        var arry1 = res.data[0].fav;
        for (var i = 0; i < arry1.length; i++) {
          for (var j = i + 1; j < arry1.length; j++) {
            if (arry1[i].id == arry1[j].id) { //第一个等同于第二个，splice方法删除第二个
              arry1.splice(j, 1);
              j--;
            }
          }
        }
        console.log(arry1)
        db.collection('bluserInfo').where({
          _openid: app.globalData.openid,
        }).update({
          data: {
            fav: arry1
          }
        })
      }
    })
    wx.navigateTo({
      url: "/pages/fav/fav"
    });
  },
  more: function () {
    var that = this;
    var moreshow = that.data.moreshow;
    var moretit;
    if (that.data.moretit == '查看更多') {
      that.setData({
        moretit: '收起'
      })
    } else {
      that.setData({
        moretit: '查看更多'
      })
    }
    that.setData({
      moreshow: !that.data.moreshow
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

   /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      var area = wx.getStorageSync('area')
      var agent = wx.getStorageSync('agent')
      var page = wx.getStorageSync('page')
      return {
          title: '2022山东公务员补录考试职位表',
          path: 'pages/blzwcx/search/search/search?area=' + area + '&agent=' + agent + '&page=' + page
      }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
      var area = wx.getStorageSync('area')
      var agent = wx.getStorageSync('agent')
      var page = wx.getStorageSync('page')
      return {
        title: '2022山东公务员补录考试职位表',
        path: 'pages/blzwcx/search/search/search?area=' + area + '&agent=' + agent + '&page=' + page
    }
  }
})